<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/9/4
 * Time: 16:56
 */

namespace app\api\validate;


use app\lib\exception\ParameterException;

class OrderPlace extends BaseValidate
{
    protected $rule = [
        'products' =>'require|checkProducts'
    ];

    protected $singleRule = [
        'product_id'=>'require|isPositiveIntger',
        'count'=>'require|isPositiveIntger',
    ];

    protected function checkProducts($values){
        if (empty($values)){
            throw new ParameterException([
                'msg'=>'商品列表不好为空',
            ]);
        }
        if (!is_array($values)){
            throw new ParameterException([
                'msg'=>'商品参数不正确',
            ]);
        }

        foreach ($values as $value){
            $this->checkProduct($value);
        }
        return true;
    }

    protected function checkProduct($value){
        $validate = new BaseValidate($this->singleRule);
        $result = $validate->check($value);
        if(!$result){
            throw new ParameterException([
                'msg' => '商品列表参数错误',
            ]);
        }
    }
}